﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using Microsoft.Office.Tools.Excel;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;

namespace MidasPoster
{
    public partial class ThisWorkbook
    {
        public QuestionForm gForm_Question = null;
        private void ThisWorkbook_Startup(object sender, System.EventArgs e)
        {
            Globals.ThisWorkbook.Application.SheetSelectionChange += new Excel.AppEvents_SheetSelectionChangeEventHandler(Application_SheetSelectionChange);
        }
        public Excel.Worksheet GetActiveWorkSheet()
        {
            Microsoft.Office.Interop.Excel.Workbook workBook = this.Application.ActiveWorkbook;
            Excel.Worksheet workSheet = (Excel.Worksheet)workBook.ActiveSheet;
            return workSheet;
        }
        private void ThisWorkbook_Shutdown(object sender, System.EventArgs e)
        {
        }
        private void Application_SheetSelectionChange(object sender, Excel.Range Target)
        {
            if ((Control.ModifierKeys & Keys.Control) == Keys.Control)
            {
                int iRow = Target.Row;
                int iCol = Target.Column;

                Excel.Worksheet workSheet = GetActiveWorkSheet();
                if (Globals.ThisWorkbook.gForm_Question != null)
                {

                    Excel.Range myRange = (Excel.Range)workSheet.Cells[iRow, 1];
                    Excel.Range myRange2 = (Excel.Range)workSheet.Cells[iRow, 2];
                    
                    MDTB_QUESTION _question = new MDTB_QUESTION();
                    _question.QUESTIONDESC = myRange.Text;
                    _question.USERID_ASK = myRange2.Text;
                    Globals.ThisWorkbook.gForm_Question.SetData(_question);
                    Globals.ThisWorkbook.gForm_Question.ShowDialog();
                    //Globals.ThisWorkbook.gForm_Question.TopMost = true;
                }
                else
                {
                    Globals.ThisWorkbook.gForm_Question = new QuestionForm();
                    Excel.Range myRange = (Excel.Range)workSheet.Cells[iRow, 1];
                    Excel.Range myRange2 = (Excel.Range)workSheet.Cells[iRow, 2];

                    MDTB_QUESTION _question = new MDTB_QUESTION();
                    _question.QUESTIONDESC = myRange.Text;
                    _question.USERID_ASK = myRange2.Text;
                    Globals.ThisWorkbook.gForm_Question.SetData(_question);
                    Globals.ThisWorkbook.gForm_Question.ShowDialog();
                    //Globals.ThisWorkbook.gForm_Question.TopMost = true;
                }
            }
            
        }
        #region VSTO Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisWorkbook_Startup);
            this.Shutdown += new System.EventHandler(ThisWorkbook_Shutdown);
        }

        #endregion

    }
}
